#include "Vector2D.h"


Vector2D Vector2D::Add(const Vector2D& left,const Vector2D& right)
{
	Vector2D v = {left.x + right.x, left.y + right.y};

	return v;
}

Vector2D Vector2D::Substract(const Vector2D& left, const Vector2D& right)
{
	Vector2D v = {left.x - right.x, left.y - right.y};

	return v;
}

Vector2D Vector2D::Scale(const Vector2D& vect,const float scale)
{
	Vector2D v = {vect.x * scale, vect.y * scale};

	return v;
}

void Vector2D::MoveBy(const Vector2D& by, Vector2D& toMove)
{
	toMove = Add(toMove, by);
}

float Vector2D::GetPolarAngleDeg(const Vector2D& vect)
{
	float angle = atan2(vect.y, vect.x) * 180 / PI;

	if(angle < 0) {angle += 360;}

	return angle;
}

float Vector2D::AngleBetweenDeg(const Vector2D& left, const Vector2D& right)
{
	return GetPolarAngleDeg(Substract(right, left));
}

float Vector2D::Length(const Vector2D& vect)
{
	return sqrt(LengthSq(vect));
}

float Vector2D::LengthSq(const Vector2D& vect)
{
	return vect.x * vect.x + vect.y * vect.y;
}

bool Vector2D::Normalize(Vector2D& vect)
{
	bool ok = true;
	float length = Length(vect);

	if(length != 0)
	{
		vect.x /= length;
		vect.y /= length;
	}
	else 
	{
		ok = false;
	}

	return ok;
}

void Vector2D::ScaleBy(Vector2D& vect, float scale)
{
	vect = Scale(vect, scale);
}

Vector2D Vector2D::GetVectorByAngleDeg(float angle)
{
	Vector2D v = {cos(angle*PI/180), sin(angle*PI/180)};

	return v;
}

bool Vector2D::Equals(const Vector2D& left, const Vector2D& right)
{
	return fabs(left.x - right.x) < EPSILON && fabs(left.y - right.y) < EPSILON;
}
